package com.springbootnetty.utils;

import com.google.common.primitives.Bytes;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.*;

public class Jt808Util {


    /**
     * JT/T808校验码计算（按字节异或求和）
     * 去头尾7E 和效验码
     * 比较数据里的效验码和生成的效验码
     * -1数据非法
     */
    public static boolean getRes(byte[] data,int checksum) {
        if (checksum==-1)return false;
        int checkCode = 0xff & data[data.length - 2];
        if (checkCode == checksum) return true;
        return false;
    }


    /**
     * JT/T808校验码计算（按字节异或求和）
     * 去头尾7E 和效验码
     * 根据数据获取效验码 一byte
     * -1数据非法
     * 占定不能大于300byte
     */
    public static int getXorCode(byte[] data) {
        if (null==data||data.length<14||data.length>300)return -1;
        int checksum = 0;
        for (int i = 1; i < data.length - 2; i++) {
            int a = 0xff & data[i];//解决负数>128情况
            checksum = checksum ^ a;
        }
        return checksum;
    }

    /**
     * JT/T808 4字节数组转经纬度
     *
     */
    public static double byte4ToBigDecimal(byte[] data) {
        int value = 0;
        // 由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (data[i] & 0x000000FF) << shift;// 往高位游
        }
        BigDecimal location = new BigDecimal(value);
        BigDecimal divide = location.divide(new BigDecimal(1000000), 6, BigDecimal.ROUND_HALF_UP);
        double rest = divide.setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
        System.out.println(rest);
        return rest;
    }


    /**
     *JT/T808 2字节数组转 协议类型
     *
     */
    public static int byte2ToInt(byte[] data) {
        return (int) ((data[0] & 0xff) << 8 | (data[1] & 0xff));
    }

    /**
     * @see []数组转换为String字符串
     * @author
     * @date 2014年5月5日 17:15:42
     * @param data byte数组
     * @return String 转换后的字符串
     */
    public static String byte6ToString(byte[]data){
        String result="";
        for (int i = 0; i < data.length; i++) {
            result+=Integer.toHexString((data[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3);
        }
        return result;
    }



    /**
     * 消息头、消息体、校验码中0x7D 0x02反转义为0x7E，0x7D 0x01反转义为0x7D
     *16进制字符串转字节数组
     * @param
     * @param
     */
    public static byte[] unescape(byte[] bytes) {
        List<Byte> byteList = new ArrayList<>();
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] == 0x7D) {
                if(bytes[i+1]==0x02){
                    byte a=0x7E;
                    byteList.add(a);
                    i++;
                    continue;
                }
                if(bytes[i+1]==0x01){
                    byte b=0x7D;
                    byteList.add(b);
                    i++;
                    continue;
                }
            }
            byteList.add(bytes[i]);
        }

        byte[] bytes2= Bytes.toArray(byteList);
        return bytes2;
    }

    /**
     * gps定位时间封装
     * @param
     * @param
     */
    public static Date gpsTime(String time) {
        String y = time.substring(0, 6);
        y = "20"+y.replaceAll("(.{2})", "-$1").substring(1);
        String s = time.substring(6, time.length());
        s = s.replaceAll("(.{2})", ":$1").substring(1);
        time=y+" "+s;
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        try {
             date = format.parse(time);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("gps时间转换异常.....");
        }
        return date;
    }

    public static void main(String[] args) {
        byte[] bytes = {0x01,0x02,0x7D,0x02,0x7D,0x00,0x03,0x04};
        byte[] unescape = unescape(bytes);
        System.out.println(Arrays.toString(unescape));
        String s="220523153017";
        String y = s.substring(0, 6);
        y = "20"+y.replaceAll("(.{2})", "-$1").substring(1);
        s = s.substring(6, s.length());
        s = s.replaceAll("(.{2})", ":$1").substring(1);
        System.out.println(y);
        System.out.println(s);
        System.out.println(y+" "+s);
    }


}
